
#include "LPC23xx.h"
#include "io.h"
#include "sys_config.h"
#include "utils.h"
#include "Analog.h"
#include "debug.h"
#include "hardware.h"
#include "iflash.h"
#include "macros.h"
#include "App.h"
#include "OS.h"

#define   SAVE_MODE		41
extern unsigned char SysState; 


#define   ATD_DELAY() NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP() //72 times	

int ATD_Result, V_Reference;

static void atd_SelectChannel ( int ChannelNumber )
{	
	AD0CR &= 0xFFFFFF00;
	AD0CR |= ( 1UL << (ChannelNumber & 0x07) );
}

int PrevResult, CurResult, Count = 0;


/* P0.23/AD0.0/I2SRX_CLK/CAP3.0 */
/* P0.24/AD0.1/I2SRX_WS/CAP3.1 */
/* P1.31/AD0.5/SCK1 */

/* void set_apin ( void ) */
/* { */
/* 	PINSEL1 |= (1UL<<16);		  /\* ATD 1 P0.24 *\/ */
/* 	PINSEL3 |= (3UL<<30);		  /\* ATD 5 P1.31 *\/ */
	
/* } */

void AnalogInit( void )
{
	PINMODE1 |= (2UL<<14);			  /* P0.23 - Vin; no pull-up no pull-down */
//	PINMODE1 |= (2UL<<20);			  /* P0.26 - Vbat; no pull-up no pull-down */

	PINSEL1 |= (1UL<<14);		  /* ATD 0 P0.23 - Vin */
	PINSEL1 |= (1UL<<16);		  /* ATD 1 P0.24 */
	PINSEL3 |= (3UL<<30);		  /* ATD 5 P1.31 */

	PCONP |= PCAD;

	AD0CR = 0; 

	AD0CR |= ((PCLK / ATD_FREQ_MAX) << ATD_CLKDIV);
	//AD0CR |= (2UL << ATD_CLKDIV);
	//AD0CR |= (2UL << ATD_CLKS);						  /* 010 9 clocks / 8 bits */
	AD0CR |= ATD_PDN;					  /* The A/D converter is operational. */

	/* V_Reference = ATD(5); */
	
	/* PrevResult = CurResult = V_Reference;   */
	/* Count = 0; */

	/* V_Reference -= (V_Reference/100) * 8; */

	/* dbg("\n V_Reference = %d\n", V_Reference);  */
}

/* #define  FWU_INTSAVE_DATA()         int save_status_reg = 0 */
/* #define  fwu_disable_interrupt()  save_status_reg = cpu_save_sr() */
/* #define  fwu_enable_interrupt()   cpu_restore_sr(save_status_reg) */


int ATD( int Channel )
{
	int Result = 0;

	FWU_INTSAVE_DATA

	fwu_disable_interrupt();

	atd_SelectChannel(Channel);
	ATD_DELAY();
	ATD_DELAY();

	ATD_START();
	ATD_DELAY();

	while ( (Result = ATD_RESULT()) == -1 )
	{
		NOP();
	}

	Result >>= 2;

	ATD_Result = Result;

	fwu_enable_interrupt();

//	DBG_OUT("\nATD=%d", ATD_Result);

	return Result;
}

#define   VIN_FALL__mVOLTS			4900	

#define   VIN_MINIMUM_IN_mVOLTS	5000	
#define   VIN_MAXIMUM_IN_mVOLTS	6200

#define   ONE_DIVISION				(3600/255)

#define   VIN_DIVIDER				2

	
//#define   VIN_MINIMUM				((VIN_MINIMUM_IN_mVOLTS/2)/13)	
#define   VIN_MINIMUM				((VIN_MINIMUM_IN_mVOLTS/VIN_DIVIDER)/ONE_DIVISION)	
#define   VIN_MAXIMUM				((VIN_MAXIMUM_IN_mVOLTS/VIN_DIVIDER)/ONE_DIVISION)

#define   VIN_FALL					((VIN_FALL__mVOLTS/VIN_DIVIDER)/ONE_DIVISION)	

#define   VIN_PERCENT_RANGE		(VIN_MAXIMUM - VIN_MINIMUM)

#define   ATD_CHAN1	0
#define   ATD_CHAN2	1
#define   ATD_CHAN3	5

int mc1, mc2, mc3;

void app_achans ( void )
{
	int c1,c2,c3;

	switch( APP.State )
	{
		case ASTE_START:
			dbg("started"); //Dima
			APP.State = ASTE_IDLE;
			OS_Sleep(20000);
			break;
       
		case ASTE_IDLE:
			c1 = ATD(ATD_CHAN1);
			c2 = ATD(ATD_CHAN2);
			c3 = ATD(ATD_CHAN3);
			dbg(""); //Dima
			if ( c1 != mc1 ) 
			{
				dbg("c1: %d", c1); //Dima
				mc1 = c1;
			}

			if ( c2 != mc2 ) 
			{
				dbg("c2: %d", c2); //Dima
				mc2 = c2;
			}

			if ( c3 != mc3 ) 
			{
				dbg("c3: %d", c3); //Dima
				mc3 = c3;
			}				
			OS_Sleep(2000);
			break; 
      

		case ASTE_FINISH:
			dbg("finish"); //Dima
			OS_Return();
       	break;
       
		default:
			;
	}
}
 


/* void Drive_Vin ( void ) */
/* { */

/* 	CurResult = ATD(5); */

/* 	/\* 6 - 4.5 = 1.5 */
/* 	   1.5V - 100% */

/* 		255 - VIN_MINIMUM = 100% */
		
		
/* 	*\/ */
	

/* 	if ( CurResult < VIN_FALL ) */
/* 	{ */
/* /\* 		PIN_SIM_SWITCH_CTRL = 1; *\/ */
/* /\* 		PIN_SIM_SWITCH_CTRL = 1; *\/ */

/* 		PIN_LCD_BACKLIGHT = 0; */
/* 		ATD_Result = 0; */
/* 		PIN_MODEM_POWER = 0; */
	
/* //		SysState = 41; */
/* 		return; */
/* 	} */
/* 	else */
/* 		if ( CurResult > VIN_MINIMUM ) */
/* 		{ */
/* 			ATD_Result = CurResult - VIN_MINIMUM; */
			
/* 			ATD_Result *= 100; //% */

/* 			ATD_Result /= VIN_PERCENT_RANGE;  */
 
/* 			if ( ATD_Result > 100 ) */
/* 			{ */
/* 				ATD_Result = 100; */
/* 			} */
/* 		} */

/* 	if ( Time_1Min ) */
/* 	{ */
/* 		DBG_OUT("\n Vinr = %d\n Vin = %d.%dV\n", CurResult, ((CurResult * ONE_DIVISION)/1000),  */
/* 				  ((CurResult * ONE_DIVISION)%1000) ); */
/* 	} */

/* } */

/* Voltage of Battery (%) */
/* 	temp = ATD_Result; */
/* 	temp *= 100;					  /\* % *\/ */
/* 	temp >>= 8;						  /\*  *\/ */
/* 	ATD_Result = temp + 1; */
